package src;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.List;
import java.util.ArrayList;

public class Main {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    public static List<Map<String, String>> dbResultToList(ResultSet rs) throws SQLException{
        ResultSetMetaData rsMetaData = rs.getMetaData();
        int numberOfColumns = rsMetaData.getColumnCount();
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        while(rs.next()){
            Map<String, String> m1 = new HashMap<String, String>();
            for (int i = 1; i < numberOfColumns + 1; i++) {
                //String fieldName = rsMetaData.getColumnName(i);
                String fieldName = rsMetaData.getColumnLabel(i);
                String cont = rs.getString(fieldName);
                m1.put(fieldName, cont);
            }
            result.add(m1);
        }
        return result;
    }
    public static List<Map<String, String>> select(HashMap<String, String> db_info, String sql){
        String db_url = db_info.get("url");
        String db_user = db_info.get("user");
        String db_pass = db_info.get("pass");
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            return dbResultToList(rs);
        } catch(SQLException se){
            se.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        } finally{
            try{
                if(stmt != null) stmt.close();
            } catch(SQLException se2){
                se2.printStackTrace();
            }
            try{
                if(conn != null) conn.close();
            } catch(SQLException se) {
                se.printStackTrace();
            }
        }
        return null;
    }
    public static List<Map<String, String>> select2(Connection conn, String sql) throws SQLException{
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        return dbResultToList(rs);
    }
    public static void tbl_find(List<Map<String, String>> data, String target, boolean equ, boolean all){
        for(int i = 0; i < data.size(); i++){
            int count = 0;
            for(Map.Entry<String, String> entry: data.get(i).entrySet()){
                String key = entry.getKey();
                String value = entry.getValue();
                String fKey = "", fValue = "";
                if(0 == count){
                    fKey = key;
                    fValue = value;
                }
                count++;
                if(equ){
                    if(value.equals(target)){
                        System.out.println("fKey: " + fKey + ", fValue: " + fValue + ", key: " + key + ", value: " + value);
                    }
                } else {
                    if(-1 != value.indexOf(target)){
                        System.out.println("fKey: " + fKey + ", fValue: " + fValue + ", key: " + key + ", value: " + value);
                    }
                }
            }
        }
    }
    public static List<List<Map<String, String>>> splitList(List<Map<String, String>> li, int n){
        List<List<Map<String, String>>> result = new ArrayList<List<Map<String, String>>>();
        result.add(new ArrayList<Map<String, String>>());
        int len_li = li.size();
        for(int i = 0; i < len_li; i++){
            result.get(result.size() - 1).add(li.get(i));
            if((i + 1) % Math.ceil((double)len_li / n) == 0 && len_li != (i + 1)){
                result.add(new ArrayList<Map<String, String>>());
            }
        }
        return result;
    }
    public static void main(String[] args) throws Exception{
        for(Entry<String, HashMap<String, String>> db: DB_info.dbs.entrySet()){
            HashMap<String, String> db_info = db.getValue();
            System.out.println();
            List<Map<String, String>> rs = select(db_info, "show tables;");
            System.out.println(splitList(rs, 3));
            /*for(int i = 0; i < rs.size(); i++){
                for(Map.Entry<String, String> entry: rs.get(i).entrySet()){
                    tbl_find(select(db_info, "select * from " + entry.getValue()), "管理员", true, true);
                }
            }*/
        }
    }
}
